<?php

namespace App\Http\Requests;

use App\Models\MemberOptionalField;
use App\Models\MerchantMemberOptionalField;
use Illuminate\Foundation\Http\FormRequest;

class EditUserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $merchant = session('web_merchant');
/*dump($merchant->id_merchant);
        $filed_ids = MerchantMemberOptionalField::where('id_merchant',$merchant->id_merchant)->pluck('id_member_optionalfield');
        dump($filed_ids);
        $fileds = MemberOptionalField::whereIn('id_member_optionalfield',$filed_ids)->get();
        dd($fileds);*/
        $fileds = $merchant->memberOptionalFields()->get()->pluck('name');

        $method=$this->method();

        $rule['emil'] = 'email';

        $rule['member_optional_name'] = 'max:80';
        if($fileds->search('name')!==false) $rule['member_optional_name'] = 'required|max:80';

        if($fileds->search('birthday')!==false) $rule['member_optional_birthday'] = 'required';

        if($fileds->search('gender')!==false) $rule['member_optional_gender'] = 'required';

        if($fileds->search('identity_code')!==false) $rule['member_optional_identity_code'] = 'required';

        $rule['member_optional_annual_income'] = 'max:80';
        if($fileds->search('annual_income')!==false) $rule['member_optional_annual_income'] = 'required|max:80';

        $rule['member_optional_child_name'] = 'max:80';
        if($fileds->search('child_name')!==false) $rule['member_optional_child_name'] = 'required|max:80';

        if($fileds->search('child_sex')!==false) $rule['member_optional_child_sex'] = 'required';

        if($fileds->search('child_birthday')!==false) $rule['member_optional_child_birthday'] = 'required';

        $rule['member_optional_favor'] = 'max:100';
        if($fileds->search('favor')!==false) $rule['member_optional_favor'] = 'required|max:100';

        $rule['member_optional_demands'] = 'max:200';
        if($fileds->search('demands')!==false) $rule['member_optional_demands'] = 'required|max:200';

        switch ($method){
            case 'POST':
                    return $rule;

                break;
            case'PATCH':
                $rule['member_optional_telephone'] = 'required|regex:/^1[3456789]\d{9}$/';
                return $rule;

                break;
        }


    }

    public function messages()
    {
        return [
            'member_optional_telephone.required'=>'手机号不能为空',
            'member_optional_telephone.regex'=>'手机号格式错误',
            'member_optional_identity_code.required'=>'身份证号不能为空',
            'member_optional_name.max'=>'用户名过长',
            'member_optional_name.required'=>'用户名不能为空',
            'member_optional_birthday.required'=>'出生日期不能为空',
            'member_optional_gender.required'=>'性别不能为空',
            'member_optional_annual_income.required'=>'年收入不能为空',

            'member_optional_child_name.max'=>'子女姓名过长',
            'member_optional_child_name.required'=>'子女姓名不能为空',
            'member_optional_child_sex.required'=>'子女性别不能为空',
            'member_optional_child_birthday.required'=>'子女生日不能为空',
            'member_optional_favor.required'=>'喜好不能为空',
            'member_optional_demands.required'=>'特殊需求不能为空',

            'email'=>'邮箱格式错误',
        ];
    }
}
